﻿namespace Pottery.Web.Helpers
{
    using System;
    using System.Collections.Generic;
    using System.Web;

    public static class CookieUtil
    {
        public const int DefaultSessionTimeout = 20000;

        /// <summary>
        /// Create a cookie
        /// </summary>
        /// <param name="cookieName"></param>
        /// <param name="value"></param>
        /// <param name="expirationDays"></param>
        public static void CreateCookie(string cookieName, string value, int? expirationDays)
        {
            HttpCookie Cookie = new HttpCookie(cookieName, value);

            if (expirationDays.HasValue)
            {
                Cookie.Expires = DateTime.Now.AddDays(expirationDays.Value);
            }

            HttpContext.Current.Response.Cookies.Add(Cookie);
        }

        /// <summary>
        /// Delete cookie using cookie's name
        /// </summary>
        /// <param name="cookieName"></param>
        public static void DeleteCookie(string cookieName)
        {
            HttpCookie Cookie = HttpContext.Current.Request.Cookies[cookieName];
            if (Cookie != null)
            {
                Cookie.Expires = DateTime.Now.AddDays(-2);
                HttpContext.Current.Response.Cookies.Remove(cookieName);
            }
        }

        /// <summary>
        /// Delete all cookies
        /// </summary>
        public static void DeleteAllCookies()
        {
            var x = HttpContext.Current.Request.Cookies;

            foreach (HttpCookie cook in x)
            {
                DeleteCookie(cook.Name);
            }
        }

        /// <summary>
        /// Check is the cookie or not
        /// </summary>
        /// <param name="cookieName"></param>
        /// <returns></returns>
        public static bool CookieExists(string cookieName)
        {
            bool exists = false;
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
            if (cookie != null)
                exists = true;

            return exists;
        }

        /// <summary>
        /// Get a dictionary with all cookies
        /// </summary>
        /// <returns>Dictionary<string, string></returns>
        public static Dictionary<string, string> GetAllCookies()
        {
            Dictionary<string, string> cookies = new Dictionary<string, string>();

            foreach (string key in HttpContext.Current.Request.Cookies.AllKeys)
            {
                cookies.Add(key, HttpContext.Current.Request.Cookies[key].Value);
            }

            return cookies;
        }

        /// <summary>
        /// Get value of the cookie
        /// </summary>
        /// <param name="cookieName"></param>
        /// <returns></returns>
        public static string GetCookieValue(string cookieName)
        {
            if (HttpContext.Current == null)
                return null;

            HttpCookieCollection cookieContainer = HttpContext.Current.Request.Cookies;

            if (cookieContainer != null)
            {
                HttpCookie cookie = cookieContainer.Get(cookieName);

                if (cookie != null)
                    return cookie.Value;
            }

            return null;
        }

        /// <summary>
        /// Set value of the cookie
        /// </summary>
        /// <param name="cookieName"></param>
        /// <param name="value"></param>
        /// <param name="expirationSeconds"></param>
        public static void SetCookieValue(string cookieName, string value, uint? expirationSeconds)
        {
            if (HttpContext.Current == null)
                return;

            HttpCookieCollection cookieContainer = HttpContext.Current.Request.Cookies;

            if (cookieContainer != null)
            {
                if (expirationSeconds.HasValue)
                {
                    cookieContainer.Set(new HttpCookie(cookieName)
                    {
                        Value = value,
                        Expires = DateTime.Now.AddSeconds(expirationSeconds.Value)
                    });
                }
                else
                {
                    cookieContainer.Set(new HttpCookie(cookieName)
                    {
                        Value = value
                    });
                }
            }
        }
    }
}